package com.bw.flinkstreaming.transform1;
import com.bw.flinkstreaming.source1.job2.NotParallelSource;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.ConnectedStreams;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoMapFunction;

/**
 * Connection在合并的时候可以数据类型不一致
 */
public class ConnectionTest {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //数据源1

        DataStreamSource<Long> data1 = env.addSource(new NotParallelSource()).setParallelism(1);

        //数据源2
        DataStreamSource<Long> data2 = env.addSource(new NotParallelSource()).setParallelism(1);

        SingleOutputStreamOperator<String> data2_s = data2.map(new MapFunction<Long, String>() {
            @Override
            public String map(Long value) {
                return "s" + value;
            }
        });

        //合并数据源
        ConnectedStreams<Long, String> connect = data1.connect(data2_s);


        /**
         * Long：数据源1的类型
         * String:数据源2的类型
         * Object：输出的数据类型
         */
        SingleOutputStreamOperator<Object> result = connect.map(new CoMapFunction<Long, String, Object>() {
            @Override
            public Object map1(Long aLong) {
                return aLong + "10086";
            }

            @Override
            public Object map2(String s) {
                return s+"map2";
            }
        });

        result.print().setParallelism(1);
        env.execute("ConnectionTest");

    }
}
